<?php

namespace App\Models;

class MoneyCategory extends Model
{
    protected $table = 'money_categories';

    protected $fillable = [
        'book_id',     // 新增账本ID字段
        'name',
        'icon',
        'type', // 1 收入 或 2 支出
        'sort',
        'description',
    ];

    // 类型常量
    const TYPE_INCOME = 1; // 收入
    const TYPE_EXPENSE = 2; // 支出
    const type_key_map = [
        'income' => self::TYPE_INCOME,
        'expense' => self::TYPE_EXPENSE,
    ];

    /**
     * 获取该分类所属的账本
     */
    public function book()
    {
        return $this->belongsTo(MoneyBook::class, 'book_id');
    }

    /**
     * 获取该分类下的所有子分类
     */
    public function subCategories()
    {
        return $this->hasMany(MoneySubCategory::class, 'category_id')->orderBy('sort');
    }

    /**
     * 获取该分类下的所有交易记录
     */
    public function transactions()
    {
        return $this->hasMany(MoneyTransaction::class, 'category_id');
    }
}
